//
//  UIImageView+XHLKit.m
//  XHLKit
//
//  Created by qinyulun on 16/6/18.
//  Copyright © 2016年 leTian. All rights reserved.
//

#import "UIImageView+XHLKit.h"

@implementation UIImageView (XHLKit)

//改变图片颜色
- (UIImage *)imageWithColor:(UIColor *)color image:(NSString *)image
{
    UIGraphicsBeginImageContextWithOptions(self.frame.size, NO,2);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, self.frame.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGRect rect = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    
    UIImage *loadImage=[UIImage imageNamed:image];
    CGImageRef cgimage=loadImage.CGImage;
    CGContextClipToMask(context, rect,cgimage);
    [color setFill];
    CGContextFillRect(context, rect);
    UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}


- (UIImage *)imageWithColor:(UIColor *)color UIImage:(UIImage *)image
{
    UIGraphicsBeginImageContextWithOptions(self.frame.size, NO,2);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, self.frame.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGRect rect = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    
    CGImageRef cgimage=image.CGImage;
    CGContextClipToMask(context, rect,cgimage);
    [color setFill];
    CGContextFillRect(context, rect);
    UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

+ (UIImageView *)ImageViewWithRect:(CGRect)rect imageName:(NSString *)name tag:(int )tag parentId:(id)body;
{
    UIImageView *img = (UIImageView *)[body viewWithTag:tag];
    if (!img) {
        img=[[UIImageView alloc]initWithFrame:rect];
        img.image=[UIImage imageNamed:name];
        img.tag=tag;
    }
    else
    {
        img.image=[UIImage imageNamed:name];
        img.tag=tag;
        img.frame=rect;
    }
    
    return img;
}

- (void)fillImage
{
    self.contentMode = UIViewContentModeScaleAspectFill;
    self.clipsToBounds = YES;
}

- (void)changeCircleCornerRadius:(CGFloat )cornerRadius andborderWidth:(CGFloat)width
{
    self.layer.cornerRadius=cornerRadius;
    self.layer.masksToBounds=YES;
    self.layer.borderWidth=width;
    self.layer.borderColor=[UIColor whiteColor].CGColor;
}

@end
